package main

import (
	"../pkg/model"
	"fmt"
	"github.com/golang/protobuf/proto"
)

/**
 * Created with Goland.
 * User: 令狐飞侠
 * Date: 2022-2-9
 * Description: 驱动函数 介绍Protocol Buffers的编码和解码
 */
func main() {
	fmt.Println("--------------编码begin--------------")
	var p = &model.Person{
		Name: "令狐冲",
		Age:  40,
	}
	data,err:= proto.Marshal(p)
	if err!=nil{
		fmt.Println("序列化错误:", err.Error())
	}
	fmt.Println("序列化成功.",data)
	fmt.Println("--------------编码end--------------")

	fmt.Println("--------------解码begin--------------")
	newPerson := model.Person{}
	err = proto.Unmarshal(data, &newPerson)
	if err!=nil{
		fmt.Println("反序列化错误:", err.Error())
	}
	fmt.Println("反序列化成功:")
	fmt.Println("name:",newPerson.Name)
	fmt.Println("--------------解码end--------------")
}